package cn.tedu.egmvc3.controller;

import cn.tedu.egmvc3.base.response.JsonResult;
import cn.tedu.egmvc3.base.response.StatusCode;
import cn.tedu.egmvc3.mapper.UserMapper;
import cn.tedu.egmvc3.pojo.dto.UserParam;
import cn.tedu.egmvc3.pojo.entity.User;
import cn.tedu.egmvc3.pojo.vo.UserListVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Date;
import java.util.List;
@Api(tags = "用户管理模块")
@Slf4j
@RequestMapping("/v1/user/")
@RestController
public class UserController {
    @Autowired
    private UserMapper userMapper;
    @PostMapping("add")
    @ApiOperation("添加用户功能")
    public JsonResult add(UserParam userParam){
        log.debug("userParam="+userParam);
        User user = new User();
        BeanUtils.copyProperties(userParam ,user);
        log.debug("user = " + user);
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());
        userMapper.insertUser(user);
        log.debug("user2 = " + user);
        return new JsonResult(StatusCode.OPERATION_SUCCESS);
    }
    @GetMapping("list")
    @ApiOperation("查询用户功能")
    public JsonResult list(){
        log.debug("userListVO");
        return new JsonResult(StatusCode.OPERATION_SUCCESS,userMapper.selectList());
    }
    @PostMapping("del")
    @ApiOperation("删除用户功能")
    @ApiImplicitParam(value = "用户id",name = "id",required = true,dataType = "Long",paramType = "query")
    //@ApiImplicitParam 是干什么的
    public JsonResult del(Long id,String username){
        log.debug("id="+id);
        if(id<0){
            throw new IllegalArgumentException("id值非法");
        }
        int num = userMapper.deleteUserById(id);
        if(num>0){
            return new JsonResult(StatusCode.OPERATION_SUCCESS);
        }else{
            return new JsonResult(StatusCode.OPERATION_FAILED);
        }
    }

    @PostMapping("update")
    @ApiOperation("更新用户功能")
    public JsonResult updateUser(@ApiIgnore UserParam userParam){
        log.debug("userParam="+userParam);
        User user = new User();
        BeanUtils.copyProperties(userParam,user);
        user.setUpdateTime(new Date());
        int i = userMapper.updateUser(user);
        if(i>0){
            return new JsonResult(StatusCode.OPERATION_SUCCESS);
        }else{
            return new JsonResult(StatusCode.OPERATION_FAILED);
        }
    }

}
